package com.cykj.eurekaribbon.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cykj.eurekaribbon.bean.FrontUser;
import com.cykj.eurekaribbon.service.FrontUserService;
import com.cykj.eurekaribbon.utils.image_detection.ImageModeration;
import com.cykj.eurekaribbon.utils.image_detection.ImageModerationBean;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;


/**
 * @version 1.0
 * @author: zhiMing
 * @date: 2021/9/24 17:00
 * @desc: 处理有关前端用户登录请求的控制层
 */
@RestController
public class FrontUserController {

    @Autowired
    FrontUserService frontUserService;

    /**
     * @version 1.0
     * @author: zhiMing
     * @date: 2021/9/24 17:00
     * @desc: 处理有关前端用户登录请求,将请求提交给service层
     */
    @RequestMapping("frontUserLogin")
    public Map getFrontUserInfo(@RequestBody Map<String,String> data){
       return frontUserService.frontUserLogin(data);
    }

    /**
     * @version 1.0
     * @author: zhiming
     * @date: 2021/9/27 00:14
     * @desc: 获取乘客上传的头像数据,由消费者端进行审核，判断图片是否异常，无异常则提交给后台
     */
    @RequestMapping("/getFrontUserAvatarUrl")
    public String getFrontUserAvatarUrl(@RequestBody Map<String ,String> data){
//        System.out.println(fileContent);
        String fileContent = data.get("fileContent");
        String fileUrl = data.get("fileUrl");
        FrontUser frontUser = null;
        System.out.println(data.get("userInfo"));
        if (data.get("userInfo") != null &&!data.get("userInfo").equals("")){

            frontUser = JSON.parseObject(data.get("userInfo"),FrontUser.class);
        }
//        System.out.println(fileContent);
//        System.out.println(fileUrl);
        boolean bool = false;

        if (fileContent!=null || fileUrl!=null){
            String resMsg = ImageModeration.postImageModeration(fileContent,fileUrl,null);
//            System.out.println(resMsg);
            ImageModerationBean moderationBean = JSON.parseObject(resMsg,ImageModerationBean.class);
            if (moderationBean.getSuggestion().equals("Pass")){
                System.out.println("验证通过");
                bool = true;
            }
        }
        if (bool && frontUser!=null){
           return JSON.toJSONString(frontUserService.getFrontUserAvatarUrl(data));
        }else if (frontUser == null){
            return "-101";
        }
        return "-200";
    }
}
